package com.minelx.serverandclient.common;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ReflectionUtils {

	public static <T> T newInstance(Class<T> aClass, Object...params) {
		List<? extends Class<?>> collect = Arrays.asList(params).stream().map(element -> element.getClass()).collect(Collectors.toList());
		Class<?>[] array = collect.toArray(new Class<?>[0]);
		try {
			Constructor<T> constructor = aClass.getConstructor(array);
			return constructor.newInstance(params);
		} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
